Jrun Article

JRun: Using JMX in the Real World

Drew Falkman

Drew Falkman

www.drewfalkman.com

Macromedia JRun 4 brought with it a hoard of new features; one of the most dramatic and least talked about features is the Java Management eXtensions (JMX) service architecture. JMX is a Java standard created specifically for monitoring and managing J2EE application services. Not only does JRun use JMX as its internal architecture—all of its services are JMX-based—but JRun developers can also create their own services and tie them into the JRun server using JMX. This provides developers with a heap of new opportunities that are worth looking into.

Introduction to JMX

To get started, let's first take a look at what JMX is. JMX, as many J2EE technologies do, started in the Java Community Process. While JMX is part of the upcoming J2EE 1.4 release, JMX has been part of JRun since JRun 4 was released last year. JMX, as its name implies, is an extensible API for managing services in the JRun server. The implications of this architecture are numerous, but ultimately provide the following interesting options:

  1. Developers can use JMX to control and monitor JRun server.
  2. Developers can plug third-party services into JRun server.
  3. Developers can create integrated services that work with JRun server.

More specifically, JMX allows you to access and change configuration parameters, load or initialize an application or its components, and monitor services. JMX monitoring can use existing monitoring interfaces such as SNMP ( Simple Network Monitoring Protocol) or HTTP (HyperText Transfer Protocol interfaces), but also offers better security and tighter integration with Java applications.

In general, enterprise applications are continually growing and evolving. This makes application monitoring and management an increasingly difficult task. JMX eases this problem by providing a management and monitoring interface that is intuitive and integrated for Java developers, flexible, and adaptive so you can monitor of all sorts of Java constructs, including EJBs, servlets, and other classes.

How JMX Works: The Short Version

The primary purpose of JMX is to manage resources. Manageable resources can range from simple Java classes, to applications like JRun server itself, to network resources like a printer. A Managed Bean, or MBean manages each of these resources. MBeans are the heart of JMX magic. MBeans are Java classes that expose services to the JMX interface. JRun loads and manages these MBeans; as such, you can access them remotely in much the same ways that you access EJBs—only through the JMX API, and of course, using the security constructs therein. Together, the managed resources and MBeans comprise the JMX Instrumentation Layer.

In this layer, MBeans talk directly to the services, each MBean having an inherent lifecycle that includes the following stages:

  1. init: Initializes parameters.
  2. start: Creates a Java Naming and Directory Interface (JNDI) binding and makes the service ready for use.
  3. stop: Removes the JNDI binding and disables the service from use (though it can be restarted).
  4. destroy: Completely removes the service from memory.

The next layer of JMX is the Agent Layer. This is the MBean server in JRun. The JRun MBean server initializes services (as exposed by MBeans) and exposes them to management interfaces. These services can be initialized automatically in JRun, by using the jrun-server-directory/SERVER-INF/jrun.xml file. This file contains JRun internal services, such as JavaMail and Java Messaging Service (JMS), and is also where you specify custom and third-party services to replace or append these. MBeans are not required to use jrun.xml for initialization.

The final layer of JMX is the Distributed Services Layer. This layer exposes JMX services through connectors and adaptors. These connectors and adaptors can be exposed through SNMP, HTTP or a number of other protocols. This layer allows you remote control of services, the ultimate goal of JMX.

Anyone who has used JRun has likely used the JRun Management Console (JMC). The JMC allows for developers to (among other things) deploy applications, change settings, view log files, and start/stop JRun servers. In other words, the JMC provides remote management and monitoring for JRun. In fact, and not surprisingly given its objective, many of the functions within the JMC use JMX to interact with JRun services.

Another way to interact with JRun JMX services is through the HTMLAgent Service, provides developers a view of the readable attributes of the JRun server. You can see this service by using the following steps:

  1. Uncomment the <service> tag in the jrun.xml file that contains the HTMLAgent Service.
  2. Go to http://your_jrun_server_address:HTMLAgentService_port in your browser (Note: Port 8082 is the default HtmlAgent Service port for the default server). The usernames/passwords available are listed as attributes to this <service> entry in the jrun.xml file. See the results in Figure 1.
Figure 1: The JMX HTMLAgentService

Figure 1: The JMX HTMLAgentService

This service will show you a list of all the MBeans currently registered for the server being accessed. By clicking on any service, developers can edit the attributes of the selected service: whether the services are bound to JNDI (for lookups) and a host of other parameters both general and specific to each MBean. For some MBeans, a list of MBean operations also displays. Because all of these services are in JMX, all are accessible in varying degrees using the MBean interface.

For a listing of the major services in JRun, refer to Chapter 9: Deployment Descriptors for JRun Administration in the JRun Administrator's Guide in the JRun LiveDocs. This section lists all of the services accessible through the jrun.xml descriptor and their attributes.

Using Third-Party Services in JRun

As seen in the above section, a slew of services are available through JMX—everything from the Logger Service to the ServletEngine Service and the JMSSecurityManager. Just as these are available, you can make third-party services available by plugging them into the JRun server through JMX.

You can see a good example of how this works in the PointBaseStarterService service. Using this service, developers can start the embedded JRun PointBase database. This service was written for the sample applications that come with JRun; some of them use a Pointbase database to store data. The samples developers wanted to be able to have this database start automatically when the JRun samples server started. You will find the relevant code in the jrun_servers_directory/samples/pointbase-service directory. In this directory, find three Java classes:

  • Pointbase ServiceMBean: This is the core MBean that sets up the class. Note that it implements jrunx.kernel.ServiceMBean, where it gets its JMX power.
  • Pointbase Service: This is the service interface. You monitor and manage this service through the methods and properties that you establish here. Note that this class implements the PointbaseServiceMBean, thus keeping its JMX connection.
  • PumpStreamHandler: This is a worker class that the Pointbase Service uses.

These classes form the PointBaseStarterService service. But you must do a bit more work; you must plug the service into the JRun service. In this case, you use the jrun.xml file. Now is a good time to discuss how this file works. The samples developers made the following entry to the jrun.xml file to enable the PointBaseStarterService service:

<service class="jrunx.samples.PointBaseService" name="PointBaseStarter">
  <attribute name="database">sample:9192</attribute>
  <attribute name="database">compass:9292</attribute>
  <attribute name="database">smarticket:9392</attribute>
  <attribute name="database">worldmusic:9692</attribute>
</service>

This is similar to all of the other services listed in the jrun.xml file. Two tags set the services up: <service> and <attribute>. The <service> tag class attribute specifies the class that contains the service interface, which in this case is jrun.samples.PointBaseService. This is the PointbaseService class that implemented the PointbaseServiceMBean. The name attribute specifies a name to refer to this service. The <attribute> daughter tags set attributes with the name of database. In the body of these tags is a database name followed by a port number. The service uses this information to determine which databases to initialize.

In short, plugging in third-party services to JRun is a relatively simple process. Because of this, a number of vendors create services that JRun and other JMX-compatible application servers can use to either replace their existing services (such as the logging service, JMS queue service, or even possible the EJB container), or supplement the JRun server in new and powerful ways.

Creating Integrated Services in JRun

It stands to reason that if you can plug third-party services into JRun, then you can create your own services and plug them in too. Of course, this is absolutely true. In fact, it is this ability that many developers will find the most useful aspect of JMX. By creating services, developers can enhance their ability to monitor their applications in how they function and perform, manage parts of their applications, and even monitor and manage other resources, such as routers and server software. Because this will all be coded, you determine what is (and what isn't) exposed and because you use JMX, it's a relatively simple process.

JRun requires three basic steps to create a custom service:

  1. Create the MBean. This should extend (as did the PointbaseServiceMBean) the jrunx.kernel.ServiceMBean class. This interface will include primarily get and set methods to control JMX manageable parameters.
  2. Implement the MBean. This class should implement the MBean class created in the previous step, as did the PointbaseService. It is in this class where the init, start, stop, and destroy classes are coded. This class will extend the jrunx.kernel.ServiceAdaptor class which includes these lifecycle events for developers to override and customize. Additionally, you can create other methods and properties.
  3. Add the MBean to JRun. This is generally done using the jrun.xml descriptor. Additionally, you can create a central service (called a ServiceFactory) that is initialized in the jrun.xml file, and then controls other services programmatically through the ServiceFactory.

Of course, this is just a basic overview. For more information, consult the JRun SDK Guide document in the JRun LiveDocs.

Conclusion

The JRun JMX service-based architecture provides a wealth of opportunities for developers. Using JMX, developers can control and monitor existing services in JRun, add in other third-party services, and even create their own services. This combination extends JRun and J2EE applications, gives you the tools to scale applications, and provides a better understanding and greater control of anything in the JRun application server. Sound like something worth a further look.


About the author

Drew Falkman is the author of JRun Web Application Construction Kit and co-author (with Ben Forta) of Reality ColdFusion: J2EE Integration , both published by Macromedia Press . Currently Drew offers technology planning, project management, and development services through his consulting company Veraison, LLC. Drew also speaks at events, writes for numerous publications, and teaches web development courses at Portland State University. Over the past 7 years, Drew has developed over 150 web applications in all sizes primarily using ColdFusion and Java. Most recently, Drew guided Stampede Cattle Company's technical direction as "virtual CTO," including planning and managing the development of their real-time cattle auction (www.stampedecattle.com) using Macromedia Flash Remoting and Flash Communication Server. This site was featured on NPR's "All Things Considered" and other national press venues. In addition, Drew is a member of Team Macromedia , a certified ColdFusion Developer, and a certified Macromedia instructor. You can learn more about Drew at www.drewfalkman.com .